package cn.goktech.sports.modules.countryEmbargoGoods.controller;

import cn.goktech.sports.common.annotation.RestAnon;
import cn.goktech.sports.common.entity.R;
import cn.goktech.sports.common.utils.ExcelUtils;
import cn.goktech.sports.modules.countryEmbargoGoods.entity.ImportEntity;
import cn.goktech.sports.modules.countryEmbargoGoods.service.CountryEmbargoGoodsService;
import cn.goktech.sports.modules.importDataone.entity.Standhard;
import cn.goktech.sports.modules.importDataone.entity.WeightLimit;
import cn.goktech.sports.modules.importDataone.service.impl.ImportDataoneServiceImpl;
import cn.goktech.sports.modules.sys.controller.AbstractController;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest/import")
public class ImportController extends AbstractController {

    @Autowired
    private CountryEmbargoGoodsService countryEmbargoGoodsService;

    @Autowired
    private ImportDataoneServiceImpl importDataoneServiceImpl;

    /**
     * 功能描述: <br>
     * 中速-TNT全球快递导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author Ysm
     * @date 2019/10/14 17:47
     */
    @ResponseBody
    @RequestMapping(value = "TNTGlobal", method = {RequestMethod.POST})
    @RestAnon
    @ApiOperation("中速-TNT全球快递导入")
    public R MediumSpeed_TNTGlobalExpress(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        Map<String, Long> productMap = countryEmbargoGoodsService.getProductMap();
        if (productMap.get(strings.get(0)[0]) == null) {
            return R.error("产品名称有误");
        }
        countryEmbargoGoodsService.delete(productMap.get(strings.get(0)[0]));
        Map<String, Standhard> standhardMap = importDataoneServiceImpl.getStandhardMap();
        Map<Double, WeightLimit> weightLimitMap = importDataoneServiceImpl.getWeightLimitMap(strings.get(0)[0]);
        if (standhardMap.get(strings.get(0)[0]) == null) {
            return R.error("请先导入尺寸标准数据");
        }
        List<ImportEntity> list = new ArrayList<>(strings.size());
        for (int i = 0; i < strings.size(); i++) {
            if (i < 4) continue;//前三位标题
            String[] array = strings.get(i);
            String[] Country = array[0].split("、");
            if (Country.getClass().isArray()) {
                for (String name : Country) {
                    ImportEntity importEntity = new ImportEntity();
                    //国家名字
                    importEntity.setNationalAreaName(name);
                    //类型id
                    importEntity.setArticleTypeId(productMap.get(strings.get(0)[0]));
                    //文件的起重价格
                    BigDecimal number = new BigDecimal(array[1]);
                    importEntity.setFilePrice(number);
                    //物品的起重价格
                    BigDecimal number2 = new BigDecimal(array[2]);
                    importEntity.setGoodsPrice(number2);
                    //续重id
                    if (weightLimitMap.get(Double.valueOf(array[5])) == null) {
                        return R.error("请先导入限重数据");
                    }
                    importEntity.setContinuousWeightId(weightLimitMap.get(Double.valueOf(array[5])).getWeightLimitId());
                    //货物续重价格/500g
                    BigDecimal number3 = new BigDecimal(array[3]);
                    importEntity.setFileHeavyPrice(number3);
                    importEntity.setGoodsHeavyPrice(number3);
                    //尺寸标准id
                    long standardId = standhardMap.get(strings.get(0)[0]).getStandardId();
                    importEntity.setStandardId(standardId);
                    //设置参考时效
                    if (StringUtils.isNotBlank(array[4]) && !array[4].equals("无")) {
                        importEntity.setConsultExtension(array[4]);
                    }

                    //设置燃油费率
                    if (StringUtils.isNotBlank(array[6])) {
                        importEntity.setFuelCost(BigDecimal.valueOf(Double.valueOf(array[6]) + 1));
                    }
                    //设置文件最高限重
                    if (StringUtils.isNotBlank(array[7])) {
                        importEntity.setFileWeight(Double.valueOf(array[7]));
                    }
                    list.add(importEntity);
                }
            }
        }
        countryEmbargoGoodsService.insert(list);
        return R.ok("插入成功！！");
    }

    /**
     * 功能描述: <br>
     * E邮宝导入
     *
     * @param request
     * @param file
     * @return java.lang.String
     * @author Ysm
     * @date 2019/10/14 17:47
     */
    @ResponseBody
    @RequestMapping(value = "EPostalTreasure", method = {RequestMethod.POST})
    @RestAnon
    @ApiOperation("E邮宝导入")
    public R MediumSpeed_EYBExpress(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        //实例化工具类
        ExcelUtils excelUtils = new ExcelUtils();
        //获取excel数据
        List<String[]> strings = excelUtils.readALLExcel(file);
        Map<String, Long> productMap = countryEmbargoGoodsService.getProductMap();
        if (productMap.get(strings.get(0)[0]) == null) {
            return R.error("产品名称有误");
        }
        Map<String, Standhard> standhardMap = importDataoneServiceImpl.getStandhardMap();
        countryEmbargoGoodsService.delete(productMap.get(strings.get(0)[0]));//删除原来的

        List<WeightLimit> weightLimitList = importDataoneServiceImpl.getWeightLimit(strings.get(0)[0]);
        if (standhardMap.get(strings.get(0)[0]) == null) {
            return R.error("请先导入尺寸标准数据");
        }
        Long standardId = standhardMap.get(strings.get(0)[0]).getStandardId();
        List<ImportEntity> list = new ArrayList<>(strings.size());
        for (int i = 0; i < strings.size(); i++) {
            if (i < 4) continue;//前三位标题
            String[] array = strings.get(i);
            String[] Country = array[0].split("、");
            if (Country.getClass().isArray()) {
                for (String name : Country) {
                    ImportEntity importEntity = new ImportEntity();
                    //国家名字
                    importEntity.setNationalAreaName(name);
                    //类型id
                    importEntity.setArticleTypeId(productMap.get(strings.get(0)[0]));
                    //文件的起重价格
                    BigDecimal number = new BigDecimal(array[1]);
                    importEntity.setSurcharge(number);
                    //物品的起重价格
                    BigDecimal number2 = new BigDecimal(array[2]);
                    importEntity.setGoodsPrice(number2);
                    //续重id
                    if (Double.valueOf(array[3]) != null && StringUtils.isNotBlank(array[5])) {
                        Double fristWeight = Double.valueOf(array[3]) / 1000;
                        Double limitWeight = Double.valueOf(array[5]) / 1000;
                        for (int j = 0; j < weightLimitList.size(); j++) {
                            WeightLimit w = weightLimitList.get(j);
                            if (fristWeight.equals(w.getFirstWeight()) && limitWeight.equals(w.getWeightLimitScale())) {
                                importEntity.setContinuousWeightId(w.getWeightLimitId());
                                break;
                            }
                        }
                    }
                    if (importEntity.getContinuousWeightId() == null) {
                        return R.error("请先导入限重数据");
                    }
                    //尺寸标准id
                    importEntity.setStandardId(standardId);
                    //导入时间
                    list.add(importEntity);
                }
            }
        }
        countryEmbargoGoodsService.insert(list);
        return R.ok("插入成功！！");
    }

}
